package com.msmall.community.common;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

import javax.servlet.http.HttpSession;

import com.msmall.base.util.Constants;
import com.msmall.community.common.listener.UserAgentBean;

public class OnlineUser implements java.io.Serializable {
	
	private static final long serialVersionUID = 1999895596563898254L;

	/**
	 * 未登录用户
	 */
	public static final int USER_ROLE_ANONYMOUS = 1;
	/**
	 * 前台登录用户
	 */
	public static final int USER_ROLE_USER = 2;
	/**
	 * 后台登录用户
	 */
	public static final int USER_ROLE_ADMIN = 3;
	
	public static final AtomicLong totalCounter = new AtomicLong(0);
	
	public static final AtomicLong userCounter = new AtomicLong(0);
	
	public static final AtomicLong anonymousCounter = new AtomicLong(0);
	
	private static final Map<Long, OnlineUser> onlineUsers = new ConcurrentHashMap<Long, OnlineUser>();
	
	/**
	 * 前台用户ID.当SESSION 创建时(前台，后台都是)，创建匿名用户，当前台用户登录时修改。
	 */
	private Long userid;
	
	/**
	 * 用户角色<br/>
	 * 1未登录用户<br/>
	 * 2前台登录用户<br/>
	 * 3后台登录用户<br/>
	 */
	private int role = USER_ROLE_ANONYMOUS;
	
	private UserAgentBean userAgentBean;
	
	public OnlineUser(){}

	/**
	 * 创建一个新的HttpSession
	 */
	public static synchronized void anonymousCreated(Long anonymousid, HttpSession session) {
		session.setAttribute(Constants.SESSION_USER_ID_KEY, anonymousid);
		OnlineUser onlineUser = new OnlineUser();
		onlineUser.setUserid(anonymousid, USER_ROLE_ANONYMOUS);
		OnlineUser.getOnlineUsers().put(anonymousid, onlineUser);
		OnlineUser.totalCounter.incrementAndGet();
		OnlineUser.anonymousCounter.incrementAndGet();
	}

	/**
	 * 前台用户登录
	 */
	public static synchronized void userAuthenticated(Long anonymous, Long userid, HttpSession session) {
		session.setAttribute(Constants.SESSION_USER_ID_KEY, userid);
		OnlineUser onlineUser = (OnlineUser)onlineUsers.get(anonymous);
		onlineUser.setUserid(userid, USER_ROLE_USER);
		onlineUsers.remove(anonymous);
		onlineUsers.put(onlineUser.getUserid(), onlineUser);
		
		anonymousCounter.decrementAndGet();
		userCounter.incrementAndGet();
	}
	/**
	 * 后台用户登录
	 */
	public static synchronized void adminAuthenticated(Long anonymous, Long userid, HttpSession session){
		session.setAttribute(Constants.SESSION_USER_ID_KEY, userid);
		OnlineUser onlineUser = (OnlineUser)onlineUsers.get(anonymous);
		onlineUser.setUserid(userid, USER_ROLE_ADMIN);
		onlineUsers.remove(anonymous);
		onlineUsers.put(onlineUser.getUserid(), onlineUser);
		
		anonymousCounter.decrementAndGet();
		userCounter.incrementAndGet();
	}
	/**
	 * 非登录用户退出系统
	 */
	public static synchronized void anonymousLogout(Long userid){
		if(onlineUsers.get(userid) != null){
			onlineUsers.remove(userid);
			anonymousCounter.decrementAndGet();
			totalCounter.decrementAndGet();
		}
	}
	/**
	 * 登录用户退出系统
	 */
	public static synchronized void userLogout(Long userid){
		onlineUsers.remove(userid);
		userCounter.decrementAndGet();
		totalCounter.decrementAndGet();
	}
	/**
	 * 后台登录用户退出系统
	 */
	public static synchronized void adminLogout(Long userid){
		onlineUsers.remove(userid);
		userCounter.decrementAndGet();
		totalCounter.decrementAndGet();
	}
	
	public static Map<Long, OnlineUser> getOnlineUsers() {
		return onlineUsers;
	}
	
	public Long getUserid() {
		return this.userid;
	}
	
	public void setUserid(Long userid, int role) {
		this.userid = userid;
		this.role = role;
	}

	public UserAgentBean getUserAgentBean() {
		return userAgentBean;
	}

	public void setUserAgentBean(UserAgentBean userAgentBean) {
		this.userAgentBean = userAgentBean;
	}

	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}
}
